<?php
namespace App\Http\Controllers\Admin;
use App\Models\Admin\Qrcode;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Admin\AdminAction;


class QrcodeController extends CommonController {

	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
	}




	/**
	 * 付款二维码
	 **/
	public function List(Request $request) {		
	    $qrcode = DB::table('qrcode')->where('store_id',$_SESSION['admin_uid'])->first();
	    //var_dump($qrcode); exit();
	    
	    if(empty($qrcode)){
    		//获取店铺ID和手机号
    		$store = DB::table('ruzhu_merchant_basic')->where('id',$_SESSION['admin_uid'])->first();
    		   		
    		require __DIR__.'/../phpqrcode/index.php';
    		$rootUrl = $_SERVER['DOCUMENT_ROOT']; 
    		$pngTempDir = $rootUrl.'/images/qrcode/';
    		$backDir    = $pngTempDir . '/back/';
    		$iconImgPath = $backDir . 'icon.png';
    		
    		$PhpQRCode = new \PhpQRCode();    		
    		$PhpQRCode->set('pngTempDir', $pngTempDir);
    		$PhpQRCode->set('matrixPointSize', 9);
    		
    	    $types = array('qr_name_global' => '1', 'qr_name_stored' => '2', 'qr_name_global_plus' => '3');
    	    $qr_names = array();
    	    foreach($types as $key => $type){
        	    //生成付款二维码
                //$url = 's=MjY5_13618624473';
                $url = 'http://m.hlqqg168.com/scan.php?type=' . $type . '&s=' . base64_encode($store->id) .'_' . $store->corpman_mobile;
                //var_dump($url); exit();
                $name = $store->id . '_' . $type . '_' .  $store->corpman_mobile . '.png';            
                $PhpQRCode->set('date',$url);
                $PhpQRCode->set('pngTempName', $name);
                $PhpQRCode->init();
                
                $backImgPath = $backDir . $type . '.png';                               
                $targetImg     = imagecreatefrompng($backImgPath);   
                //$targetImg     = imagecreatefromstring(file_get_contents($backImgPath));
                $qrcodeImgPath = $pngTempDir . $name;
                $qrcodeImg     = imagecreatefrompng($qrcodeImgPath);
                //$qrcodeImg     = imagecreatefromstring(file_get_contents($qrcodeImgPath));
                list($qrcodeWidth, $qrcodeHight, $qrcodeType) = getimagesize($qrcodeImgPath);
                $iconImg = imagecreatefrompng($iconImgPath);
                list($iconWidth, $iconHight, $iconType) = getimagesize($iconImgPath);
               
                //imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);
                $width  = 175;
                $height = 370;
                imagecopymerge($targetImg, $qrcodeImg, $width, $height, 0, 0, $qrcodeWidth, $qrcodeHight, 100);
                imagecopymerge($targetImg, $iconImg, $width + $qrcodeWidth/2 - $iconWidth/2, $height + $qrcodeHight/2 - $iconHight/2, 0, 0, $iconWidth, $iconHight, 100);
                
                // Save the image to file and free memory
                imagepng($targetImg, $qrcodeImgPath);
                imagedestroy($targetImg);
                imagedestroy($qrcodeImg); 
                imagedestroy($iconImg);
                
                $qr_names[$key] = $name;
    	    }
    	    
    	    //生成二维码插入数据表
    	    $data = array(
    	        'id' => Qrcode::getNextSeq(),
    	        'store_id' => $store->id,
    	        'store_phone' => $store->corpman_mobile,
    	        'qr_name_global' => $qr_names['qr_name_global'],
    	        'qr_name_stored' => $qr_names['qr_name_stored'],
    	        'qr_name_global_plus' => $qr_names['qr_name_global_plus'],
    	    );
    	    $res = DB::table('qrcode')->insert($data);
	    }else{
	        $qr_names = array(
	            'qr_name_global' => $qrcode->qr_name_global,
	            'qr_name_stored' => $qrcode->qr_name_stored,
	            'qr_name_global_plus' => $qrcode->qr_name_global_plus,
	        );
	    }
	    
        //echo "<script>alert('二维码生成成功!')</script>";
		return view('admin.qrcode.list', ['qr_names' => $qr_names]);
	}
	
	public function download(Request $request)
	{
	    $type = intval($request->input('type'));
	    $key_lookup = array('1' => 'qr_name_global', '2' => 'qr_name_stored', '3' => 'qr_name_global_plus');
	    if(!in_array($type, array('1', '2', '3'))){
	        echo "<script>alert('图片不存在')</script>"; exit();
	    }
	    
	    $qrcode = DB::table('qrcode')->where('store_id',$_SESSION['admin_uid'])->first();
	    //var_dump($qrcode); exit();
	    if(empty($qrcode)){
	        echo "<script>alert('图片不存在')</script>"; exit();
	    }
	    
	    $key = $key_lookup[$type];
	    $rootUrl = $_SERVER['DOCUMENT_ROOT'];
	    $imgPath = $rootUrl.'/images/qrcode/' . $qrcode->{$key};
	        
	    return response()->download($imgPath);
	}

	
}